<template>
	<div class="alarmConfiguration zy-content">
		<search :columns="searchData" @getSearch="getSearch(arguments)"></search>
		<div class="list-layout mt16">
			<div class="btns clear">
				<span class="title">{{ $t('common.listInformation') }}</span>
				<Button v-has="'wlczCode'" @click="createAdd">{{ $t('common.New') }}</Button>
				<Button @click="handleCommonReject">撤销</Button>
				<Button @click="exportTemplateClick">{{ $t('common.batchExport') }}</Button>
			</div>
			<div class="tableContent">
				<Table
					class="resizable-table"
					border
					@on-column-width-resize="onColumnWidthResize"
					:columns="columnsList"
					ellipsis
					ref="selection"
					stripe
					:data="dataArr"
					@on-selection-change="onSelectChange"
				>
					<template slot-scope="{ row }" slot="action">
						<span class="operationBtn" v-if="row.approvalResults == '待审批'" v-has="'materialAdopt'" @click="materialApproval(row, true)"
							><i class="iconfont zy-common-jinyong"></i>
							通过
						</span>
						<span class="operationBtn" v-if="row.approvalResults == '待审批'" v-has="'materialReject'" @click="materialApproval(row, false)"
							><i class="iconfont zy-common-jinyong"></i>
							驳回
						</span>
					</template>
				</Table>
			</div>
			<div class="pageWrap mt16">
				<pageLeftTitle :total="total" :selected="delIds.length" />
				<Page
					:total="total"
					:page-size="pageSize"
					:page-size-opts="pageSizeOption"
					:current="pageNum"
					@on-change="changePageNum"
					@on-page-size-change="changePageSize"
					show-sizer
					show-total
				/>
			</div>
		</div>
		<commonModal ref="commonModal"></commonModal>
		<inputModal ref="inputModal"></inputModal>
		<addModal @refresh="searchList" ref="addModal"></addModal>
	</div>
</template>

<script>
import search from '@/components/curd/searchList.vue';
import fileDownload from 'js-file-download';
import toggleTableOperation from '@/mixins/toggleTableOperation';
import inputModal from '@/components/modalTypes/inputModal.vue';
import commonModal from '@/components/modalTypes/commonModal.vue';

import addModal from './addModal.vue';

export default {
	name: 'materialOperation',
	components: { search, inputModal, addModal, commonModal },
	mixins: [toggleTableOperation],
	data() {
		return {
			formItem: {},
			addtitle: '',
			searchParams: {},
			pageNum: 1,
			pageSize: 10,
			total: 0,

			searchData: [
				{
					title: '车间',
					key: 'functionalLocation',
					type: 'cascader',
					dicUrl: '/basic/organization/findListByToken?level=0',
					params: {
						label: 'name',
						value: 'code',
						children: 'children',
					},
				},
				{
					title: '工单号',
					key: 'workCode',
				},
				{
					title: '产品编号',
					key: 'productNumber',
				},
				{
					title: '物料编号',
					key: 'materialCode',
				},
				{
					title: '卷号',
					key: 'rollNumber',
				},
				{
					title: '规格型号',
					key: 'specificationModel',
				},
				{
					title: '物料名称',
					key: 'materialName',
				},
				{
					title: '物料批次',
					key: 'materialBatch',
				},

				{
					title: '操作时间',
					key: 'time',
					type: 'datetimerange',
					format: 'yyyy-MM-dd HH:mm:ss',
					span: 8,
				},
				{
					title: '生产时间',
					key: 'time1',
					type: 'daterange',
				},
				{
					title: '操作工',
					key: 'operator',
					dicUrl: '/basic/user/searchUser',
					type: 'search',
					queryName: 'key',
					resultParams: 'res.data',
					params: {
						label: 'realname',
						value: 'realname',
					},
				},
				{
					title: '操作类型',
					key: 'feedingStatus',
					type: 'select',
					dicData: [
						{
							label: '投料',
							value: '投料',
						},
						{
							label: '退料',
							value: '退料',
						},
					],
				},
				{
					title: '标签流水号',
					key: 'tagSerialNumber',
				},
				{
					title: '图号规格',
					key: 'customMaterialCode',
					minWidth: 130,
				},
        {
          title: '设备编号',
          key: 'deviceCode',
          minWidth: 130,
        },
			],
			columns: [
				{
					key: 'selection',
					type: 'selection',
					width: 60,
					align: 'center',
				},
				{
					title: '物料操作单号',
					key: 'id',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '车间',
					key: 'functionalLocationName',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '班次',
					key: 'shift',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '工单号',
					key: 'workCode',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '工单组号',
					key: 'groupItem',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '设备编号',
					key: 'deviceCode',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '模具编号',
					key: 'moldCode',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '产品编号',
					key: 'productNumber',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '产品名称',
					key: 'productName',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '产品型号',
					key: 'productModel',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				// {
				//   title: "计划开始时间",
				//   key: "operatorStartTime",
				//   minWidth: 150,
				//   align: "center",
				//   resizable: true,
				// },
				{
					title: '物料编号',
					key: 'materialCode',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '卷号',
					key: 'rollNumber',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '料位',
					key: 'materialLevel',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '规格型号',
					key: 'specificationModel',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '物料类型',
					key: 'materialModel',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '物料批次',
					key: 'materialBatch',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '重量',
					key: 'weight',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '单位',
					key: 'unit',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '图号规格',
					key: 'customMaterialCode',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '操作类型',
					key: 'feedingStatus',
					minWidth: 180,
					align: 'center',
					resizable: true,
					// render: (h, params) => {
					//   return h(
					//     "span",
					//     params.row.feedingStatus == "加料" ? "投料" : "退料"
					//   );
					// },
				},
				{
					title: '投料时间',
					key: 'feedingTime',
					minWidth: 150,
					align: 'center',
					resizable: true,
					render: (h, params) => {
						return h('span', params.row.feedingTime ? params.row.shiftTime : '--');
					},
				},
				{
					title: '退料时间',
					key: 'returnTime',
					minWidth: 150,
					align: 'center',
					resizable: true,
					render: (h, params) => {
						return h('span', params.row.returnTime ? params.row.shiftTime : '--');
					},
				},
				{
					title: '标签流水号',
					key: 'tagSerialNumber',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '调拨单号',
					key: 'productInCode',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '操作人',
					key: 'operator',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '操作时间',
					key: 'operatorTime',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '审批人',
					key: 'approvalOperator',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '审批原因',
					key: 'reasonForApproval',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},

				{
					title: '审批时间',
					key: 'approvalTime',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '审批状态',
					key: 'approvalResults',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '备注',
					key: 'remarks',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '撤销人',
					key: 'revocationBy',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '撤销时间',
					key: 'revocationTime',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '撤销原因',
					key: 'reasonForRevocation',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '物料凭证',
					key: 'mblnr',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '操作',
					slot: 'action',
					fixed: 'right',
					minWidth: 200,
					align: 'center',
				},
			],
			dataArr: [],
			delFaultCodeShowF: false, //是否显示删除弹窗
			pageSizeOption: [10, 20, 30, 40],
			delIds: [],
			selection: [],
		};
	},
	mounted() {
		this.getTableColumnsWidth(this.columns);
		this.searchList();
	},
	methods: {
		onSelectChange(selection) {
			let myArray = [];
			selection.map((item, index) => {
				myArray.push(item);
			});
			this.delIds = myArray;
		},
		getSearch(arg) {
			this.searchParams = JSON.parse(JSON.stringify(arg[0]));
			if (this.searchParams.time && this.searchParams.time.length > 0) {
				this.searchParams.operatorStartTime = this.searchParams.time[0];
				this.searchParams.operatorEndTime = this.searchParams.time[1];
				delete this.searchParams.time;
			}
			if (this.searchParams.time1 && this.searchParams.time1.length > 0) {
				this.searchParams.startTime = this.searchParams.time1[0];
				this.searchParams.endTime = this.searchParams.time1[1];
				delete this.searchParams.time1;
			}
			const loca = this.searchParams.functionalLocation;
			if (loca && loca.length !== 0) {
				this.searchParams.functionalLocation = [loca.at(-1)];
			}
			this.pageNum = 1;
			this.searchList();
		},
		handleCommonReject() {
			if (this.delIds.length !== 1) {
				this.$Message.warning('请选择一条数据进行操作！');
				return;
			}
			this.$refs.inputModal.showModal({
				title: '撤销',
				submit: (form) => {
					this.axios({
						method: 'post',
						url: '/dms/mobileEnd/revokeMaterial',
						data: {
							id: this.delIds[0].id,
							reasonForRevocation: form.value,
						},
					}).then((res) => {
						if (res.code === 200) {
							this.$Message.success(res.msg);
							this.searchList();
						} else {
							this.$Message.warning({
								content: res.msg,
								duration: 6,
								closable: true,
							});
						}
					});
				},
			});
		},
		materialApproval(row, type) {
			// if (this.delIds.length !== 1) {
			// 	this.$Message.warning('请选择一条数据进行操作！');
			// 	return;
			// }
			if (type) {
				this.$refs.commonModal.showModal({
					title: '通过确认',
					type: 'sure',
					desc: '是否确认通过？',
					submit: () => {
						this.axios({
							method: 'post',
							url: '/dms/materialOperation/materialApproval',
							data: {
								id: row.id,
								status: '成功',
							},
						}).then((res) => {
							if (res.code === 200) {
								this.$Message.success(res.msg);
								this.searchList();
							} else {
								this.$Message.warning({
									content: res.msg,
									duration: 6,
									closable: true,
								});
							}
						});
					},
				});
			} else {
				this.$refs.inputModal.showModal({
					title: '驳回原因',
					submit: (form) => {
						this.axios({
							method: 'post',
							url: '/dms/materialOperation/materialApproval',
							data: {
								id: row.id,
								reasonForApproval: form.value,
								status: '失败',
							},
						}).then((res) => {
							if (res.code === 200) {
								this.$Message.success(res.msg);
								this.searchList();
							} else {
								this.$Message.warning({
									content: res.msg,
									duration: 6,
									closable: true,
								});
							}
						});
					},
				});
			}
		},
		createAdd() {
			this.$refs.addModal.show();
		},
		reset() {
			this.searchParams = {};
			this.changeNum();
		},
		//分页
		changeNum() {
			this.pageNum = 1;
			this.searchList();
		},
		changePageSize(pageSize) {
			this.pageSize = pageSize;
			this.searchList();
		},

		changePageNum(val) {
			this.pageNum = val;
			this.searchList();
		},
		//查询列表
		searchList() {
			let params = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				...this.searchParams,
			};
			this.axios({
				method: 'post',
				url: '/dms/mobileEnd/queryMaterialOperationRecordList',
				data: params,
			}).then((res) => {
				if (res.code === 200) {
					this.dataArr = res.data.records;
					this.total = res.data.total;
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},
		//导出
		exportTemplateClick() {
			//   let params = {
			//   };
			//   for (let key in this.searchParams) {
			//       if (this.searchParams[key]) {
			//           this.$set(params, key, this.searchParams[key])
			//       }
			//   }
			const filName = '物料操作记录';
			this.axios({
				url: '/dms/materialOperation/export',
				method: 'post',
				data: {
					...this.searchParams,
				},
			}).then((res) => {
				if (res.code == 555) {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				} else {
					this.axios({
						url: '/dms/materialOperation/export',
						method: 'post',
						data: {
							...this.searchParams,
						},
						responseType: 'blob',
					}).then((res) => {
						fileDownload(res, filName + '.xls');
					});
				}
			});
		},
	},
};
</script>

<style scoped lang="scss">
.ivu-input-number {
	width: -webkit-fill-available;
}

.row_click {
	cursor: pointer;
	color: rgb(50, 113, 240) !important;
}
</style>
